<template>
  <div class="container">
    <div class="app-container">
      <div class="role">
        <el-button type="primary" size="mini" @click="dialogVisible = true">添加角色</el-button>
      </div>
      <el-table :data="list">
        <el-table-column prop="name" width="200" label="角色" align="center">
          <!-- 编辑功能-角色部分 -->
          <template v-slot="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" size="mini">{{ row.name }}</el-input>
            <span v-else>{{ row.name }}</span>
          </template>

        </el-table-column>
        <!-- 编辑功能-启用部分 -->
        <el-table-column prop="state" width="200" label="启用" align="center">
          <template #default="{row}">
            <el-switch v-if="row.isEdit" v-model="row.editRow.state" :active-value="1" :inactive-value="0" />
            <span v-else>{{ row.state===0?'未启用':row.state===1?'已启用': '无' }}</span>
          </template>
        </el-table-column>
        <!-- 编辑功能-描述部分 -->
        <el-table-column prop="description" label="描述" align="center">
          <template v-slot="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" type="textarea" size="mini" />
            <span v-else>{{ row.description }}</span>
          </template>

        </el-table-column>
        <!-- 操作按钮 -->
        <el-table-column width="700" label="操作" align="center">
          <template v-slot="{row}">
            <template v-if="row.isEdit">
              <el-button type="primary" size="mini" @click="editOk(row)">确认</el-button>
              <el-button size="mini" @click="row.isEdit=false">取消</el-button>
            </template>
            <template v-else>
              <el-button size="mini" type="text" @click="showPermission(row.id)">分配权限</el-button>
              <el-button size="mini" type="text" @click="edit(row)">编辑</el-button>
              <el-button size="mini" type="text" @click="del(row.id)">删除</el-button>
            </template>

          </template>

        </el-table-column>
      </el-table>
      <!--  分页 -->
      <el-row type="flex" style="height: 60px;" align="middle" justify="end">
        <el-pagination
          layout="prev, pager, next"
          :total="roleFormList.total"
          :page-size="roleFormList.pagesize"
          :current-page="roleFormList.page"
          @current-change="pageChange"
        />
      </el-row>
    </div>
    <!-- 添加角色弹出框 -->
    <el-dialog :visible.sync="dialogVisible" title="新增角色" width="500px">
      <el-form ref="form" label-width="120px" :model="roleForm" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" size="mini" style="width:300px" />
        </el-form-item>
        <el-form-item label="启用" prop="state">
          <el-switch v-model="roleForm.state" size="mini" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" type="textarea" size="mini" :rows="3" style="width:300px" />
        </el-form-item>
        <el-row type="flex" justify="center">
          <el-button type="primary" size="mini" @click="btnOk">确认</el-button>
          <el-button size="mini" @click="close">取消</el-button>
        </el-row>
      </el-form>
    </el-dialog>
    <el-dialog title="分配权限" :visible.sync="showPermissionDialog">
      <el-tree ref="treePermission" :default-checked-keys="permIds" node-key="id" :data="permissionList" :props="{label:'name'}" check-strictly show-checkbox />
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="mini" @click="PermissionBtnOK">确认</el-button>
          <el-button size="mini" @click="showPermissionDialog=false">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleList, addRole, updateRole, delRole, getOpenList, getRoleDetail } from '@/api/role'
import { getPermissionList } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  name: 'Role',
  data() {
    return {
      list: [],
      roleFormList: {
        page: 1,
        pagesize: 5,
        total: 0

      },
      dialogVisible: false, // 弹出框状态
      roleForm: {
        name: '',
        description: '',
        state: false
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      permissionList: [], // 分配权限里面的列表
      showPermissionDialog: false, // 分配权限弹出层
      currentId: '', // 分配权限id
      permIds: []
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 获取角色信息
    async getRoleList() {
      const { rows, total } = await getRoleList(this.roleFormList)
      this.roleFormList.total = total
      this.list = rows
      // 为每个对象都添加是否显示条件
      this.list.forEach(item => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          name: item.name,
          description: item.description,
          state: item.state
        })
      })
    },
    // 切换页
    async pageChange(newPage) {
      this.roleFormList.page = newPage
      await this.getRoleList()
    },
    // 确认添加点击事件
    btnOk() {
      this.$refs.form.validate(async isOk => {
        if (isOk) {
          await addRole(this.roleForm)
          await this.getRoleList()
          this.close()
        }
      })
    },
    //  关闭
    async close() {
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    // 编辑角色
    edit(row) {
      row.isEdit = true
      row.editRow.name = row.name
      row.editRow.description = row.description
      row.editRow.state = row.state
    },
    // 确认修改
    async editOk(row) {
      if (row.editRow.name && row.editRow.description) {
        await updateRole({ ...row.editRow, id: row.id })
        this.$message.success('更新成功')
        Object.assign(row, {
          ...row.editRow,
          isEdit: false
        })
      } else {
        this.$message.warning('角色和描述不能为空')
      }
    },
    del(id) {
      this.$confirm('你确认要删除吗').then(async() => {
        await delRole(id)
        this.getRoleList()
      })
    },
    async showPermission(id) {
      this.currentId = id
      this.permissionList = transListToTreeData(await getPermissionList(), 0)
      const { permIds } = await getRoleDetail(id)
      this.permIds = permIds
      this.showPermissionDialog = true
    },
    async PermissionBtnOK() {
      const res = this.$refs.treePermission.getCheckedKeys()
      await getOpenList({
        id: this.currentId,
        permIds: res
      })
      this.showPermissionDialog = false
      this.$message.success('分配成功')
    }
  }
}
</script>
<style scopde>
.role{
  padding: 10px;
}

</style>
